#!/bin/bash

echo "guess a number game start! "

echo "name: lichangsheng, id: 24055060132"

# 生成1-100的随机数
NUM1=$(( RANDOM % 100 + 1 ))

START=1
MAX=7

while [ $START -le $MAX ]; do
    # 读取用户输入
    read -p "$START/$MAX: Please input a num(1-100): " NUM2

    # 判断输入是否为数字（额外增加的健壮性判断）
    if ! [[ $NUM2 =~ ^[0-9]+$ ]]; then
        echo "Please input a valid number!"
        START=$(( START - 1 ))  # 不消耗猜测次数
    else
        # 比较数字大小
        if [ $NUM2 -eq $NUM1 ]; then
            echo "猜对了！"
            exit 0
        elif [ $NUM2 -gt $NUM1 ]; then
            echo "你输入的数字太大了"
        else
            echo "你输入的数字太小了"
        fi
    fi

    START=$(( START + 1 ))
done

# 7次都未猜对
echo "你输了！"
echo "正确的数字是: $NUM1"
exit 1
